//
//  MqForumPostViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumPostViewController.h"

@interface MqForumPostViewController ()
<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *titleTextField;

@property (nonatomic, strong) UITextView *contentTextView;

@end

@implementation MqForumPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(backItem:)];
    
    UIBarButtonItem *postBarItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:(UIBarButtonItemStylePlain) target:self action:@selector(postBarItem:)];
    self.navigationItem.rightBarButtonItem = postBarItem;
    self.navigationItem.leftBarButtonItem = backItem;
    
    //  标题
    _titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, kScreenWidth - 20, 40)];
    _titleTextField.placeholder = @"请输入标题";
    _titleTextField.backgroundColor = [UIColor lightGrayColor];
    _titleTextField.delegate = self;
    
    
    [self.view addSubview:_titleTextField];
    
    _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(_titleTextField.left, _titleTextField.bottom + 10, _titleTextField.width, 200)];
    _contentTextView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_contentTextView];
    
    
//    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"cgtitle"];
    //  草稿
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"cgtitle"]) {
        _titleTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"cgtitle"];
        _contentTextView.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"cgcontent"];
    }
}
- (void)backItem:(UIBarButtonItem *)item{
    
   
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否保存" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSUserDefaults standardUserDefaults] setObject:_titleTextField.text forKey:@"cgtitle"];
        [[NSUserDefaults standardUserDefaults] setObject:_contentTextView.text forKey:@"cgcontent"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"cgtitle"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"cgcontent"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    
    [alert addAction:action];
    [alert addAction:action2];
    
    if ([_titleTextField.text length]!= 0) {
        
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
   
    
}
//  发帖
- (void)postBarItem:(UIBarButtonItem *)barItem{
    
    
}
//  点击return按钮textField的键盘回收
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
    
}

//  点击非编辑区域textView与textField键盘回收
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    UITouch *touch = [touches anyObject];
    
    [_titleTextField endEditing:YES];
    [_contentTextView endEditing:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
